//
//  ViewControllerImpl.m
//  CPetro
//
//  Created by ggx on 2017/3/8.
//  Copyright © 2017年 高广校. All rights reserved.
//

#import "ViewControllerImpl.h"
#import "UIView+LTEmptyView.h"
#import "ViewControllerImpl+clt.h"
@interface ViewControllerImpl ()



@end

@implementation ViewControllerImpl

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.navigationItemTitle = PROJECT_NAME;//

    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.navigationController.viewControllers.count > 1) {
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(-40, 0, 40, 44)];
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [btn setImage:[UIImage imageNamed:@"fanhuihuise"] forState:UIControlStateNormal];
        [btn addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    }
}

- (void)dealloc {
#if DEBUG
    NSLog(@"----%@----dealloc", [self class]);
#endif
}

-(void)setNavigationItemTitle:(NSString *)navigationItemTitle{
    self.navigationItem.title = navigationItemTitle;
//    [self configureBarWithTtitle:navigationItemTitle];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        [self Init];
    }
    return self;
}

-(void)Init{
    if (self.edgesForExtendedLayout) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
}
-(BOOL)hidesBottomBarWhenPushed{
    if ([self isKindOfClass:NSClassFromString(@"WLHomePageController")]||[self isKindOfClass:NSClassFromString(@"WLPickiewController")]||[self isKindOfClass:NSClassFromString(@"WLOpenEyeViewController")]||[self isKindOfClass:NSClassFromString(@"UserCenterViewController")]) {
        return NO;
    }else{
        return YES;
    }
    
}

- (void)setIsUserEmptyView:(BOOL)isUserEmptyView {
    _isUserEmptyView = isUserEmptyView;
    if (_isUserEmptyView) {
        [self.baseTable showEmptyView];
    } else {
        [self.baseTable hiddenEmptyView];
    }
}
#pragma mark -网络数据
- (void)showAnimated:(BOOL)animated title:(NSString *)title whileExecutingBlock:(CGDataResult *(^)())block completionBlock:(void(^)(BOOL b,CGDataResult *r))completion{
    MBProgressHUD *hud;
    if (animated) {
        hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.label.text = title;
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        CGDataResult *result = block();
        dispatch_sync(dispatch_get_main_queue(), ^{
            completion(result.status.boolValue,result);
            [hud hideAnimated:YES];
            if (result&&[result isKindOfClass:[CGDataResult class]]) {
                NSString *msg = result.errorMsg;
                if(!result.status.boolValue) [[HUDHelper sharedInstance]tipMessage:msg delay:1.0];
            }
            
        });
    });
}

- (void)hideKeyBoard {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [[UITableViewCell alloc] init];
}

- (void)addMoreData:(NSArray *)loadArr andLastPage:(NSInteger)lastPage {
    //原始数据组装数组 新数据加入
    NSMutableArray *mutabArr = [NSMutableArray arrayWithArray:self.dataArray];
    [mutabArr addObjectsFromArray:loadArr];
    self.dataArray = mutabArr.copy;
//    完成刷新
    [self.baseTable reloadData];
    [self.baseTable.mj_footer endRefreshing];
 
    [self.wl_collectionView reloadData];
    [self.wl_collectionView.mj_footer endRefreshing];
    
    [self showLastPage:lastPage];
}

- (void)showLastPage:(NSInteger)lastPage {
    //判断最后一页
    if (self.currentPage >= lastPage) {
        self.currentPage = lastPage;
        [self.baseTable.mj_footer endRefreshingWithNoMoreData];
        [self.wl_collectionView.mj_footer endRefreshingWithNoMoreData];
    } else {
        [self.baseTable.mj_footer resetNoMoreData];
        [self.wl_collectionView.mj_footer resetNoMoreData];
    }
}

- (void)configureBarWithTtitle:(NSString *)title {
    [self.view addSubview:self.topView];
    
    [self.topView addSubview:self.titlelabel];
    [self.topView addSubview:self.leftButton];
    [self.topView addSubview:self.bottomLine];
    self.titlelabel.text = (title && title.length > 0) ? title : @"";
}

//导航栏头部视图
- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH , kHeightNavigation)];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

//导航栏左边按钮
- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _leftButton.frame = CGRectMake(0, kHeightNavigationStatusBar, 44, KNavHeight);
        [_leftButton setImage:[UIImage imageNamed:@"backImg"] forState:UIControlStateNormal];
        [_leftButton addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftButton;
}

- (void)backClick:(UIButton*)sender {
    if (self.navigationController) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

//导航栏右边按钮
- (UIButton *)rightButton {
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightButton.frame = CGRectMake(SCREEN_WIDTH - KNavHeight - 12 * Device_Ccale, kHeightNavigationStatusBar, KNavHeight, KNavHeight);
        _rightButton.titleLabel.font = [UIFont systemFontOfSize:18*Device_Ccale];
        [_rightButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_rightButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [_rightButton addTarget:self action:@selector(rightClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightButton;
}

-(void)rightClick{
    
}

#pragma mark - Getter
//导航栏标题
- (UILabel *)titlelabel {
    if (!_titlelabel) {
        _titlelabel = [[UILabel alloc]init];
        _titlelabel.frame = CGRectMake(self.leftButton.right, kHeightNavigationStatusBar, self.topView.frame.size.width - self.leftButton.right-60, KNavHeight);
        _titlelabel.center = CGPointMake(self.topView.center.x, self.titlelabel.center.y);
        _titlelabel.textAlignment = NSTextAlignmentCenter;
        _titlelabel.font = [UIFont systemFontOfSize:17 * Device_Ccale];
    }
    return _titlelabel;
}

//导航栏分割线
- (UIView *)bottomLine {
    if (!_bottomLine) {
        _bottomLine = [[UIView alloc]init];
        _bottomLine.frame = CGRectMake(0, kHeightNavigation - 1, SCREEN_WIDTH, 1);
        _bottomLine.backgroundColor = [UIColor colorWithHexString:@"#DCE1E6"];
    }
    return _bottomLine;
}

- (BaseTableView *)baseTable {
    if (!_baseTable) {
        _baseTable = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kScreenSafeAreaHeight) style:_tabStyle == 0?0:1];
        
        _baseTable.delegate = self;
        _baseTable.dataSource = self;
        
        _baseTable.rowHeight = UITableViewAutomaticDimension;
        _baseTable.estimatedRowHeight = 54;
        //
        if ([self respondsToSelector:@selector(refreshView)]) {
            _baseTable.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshView)];
        }
        if ([self respondsToSelector:@selector(loadMore)]) {
            _baseTable.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
        }
        if (@available(iOS 11.0, *)) {
            _baseTable.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        if (@available(iOS 15.0, *)){
            _baseTable.sectionHeaderTopPadding = 0;
        }
        [_baseTable showEmptyView];
    }
    return _baseTable;
}
@end
